package com.zhuiyun.project.api.dict.service.impl;

import com.zhuiyun.project.api.appuserreport.entity.UserReport;
import com.zhuiyun.project.api.dict.entity.DictType;
import com.zhuiyun.project.api.dict.mapper.DictTypeMapper;
import com.zhuiyun.project.api.dict.model.DictTypeModel;
import com.zhuiyun.project.api.dict.service.DictTypeService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.BeanUtils;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import com.zhuiyun.project.util.pageUtil.PageResult;
import com.zhuiyun.project.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @ClassName DictTypeServicerImpl
 * @Description TODO
 * @Author Root
 * @Date 2023/6/20 10:39
 **/
@Service
public class DictTypeServicerImpl implements DictTypeService {
    @Autowired
    DictTypeMapper dictTypeMapper;
    /**
    * @Author Root
    * @Description 字典类型分页查询
    * @Date 2023/6/20 10:51
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param pageRequest
    * @param dictTypeModel
    */
    @Override
    public CommonResult selectDictByPage(PageRequest pageRequest, DictTypeModel dictTypeModel) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(DictType.class);
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(dictTypeModel.getDictName())){
            example.createCriteria().andLike("dictName","%"+dictTypeModel.getDictName()+"%");
        }
        PageResult pageResult = MybatisPageHelper.getPageResult(dictTypeMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author Root
    * @Description 修改字典类型
    * @Date 2023/6/20 16:46
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictTypeModel
    */
    @Override
    public CommonResult updateDict(DictTypeModel dictTypeModel) {
        DictType dictType = new DictType();
        // dictTypeModel 对象拷贝成 dictType
        BeanUtils.copy(dictTypeModel, dictType);
        dictType.setUpdateTime(new Date());
        int i = dictTypeMapper.updateByPrimaryKeySelective(dictType);
        if (i > 0) {
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        } else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
    * @Author Root
    * @Description 删除字典类型
    * @Date 2023/6/20 21:23
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictId
    */
    @Override
    public CommonResult deleteById(Integer dictId) throws CommonException {
        if(ObjectUtils.isNotEmpty(dictId)){
            int i = dictTypeMapper.deleteByPrimaryKey(dictId);
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else {
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }
    /**
    * @Author Root
    * @Description 添加字典信息
    * @Date 2023/6/21 9:16
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictTypeModel
    */
    @Override
    public CommonResult insertDict(DictTypeModel dictTypeModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(dictTypeModel)){
            DictType dictType = new DictType();
            // dictTypeModel 对象拷贝成 dictType
            BeanUtils.copy(dictTypeModel, dictType);
            dictType.setCreateTime(new Date());
            dictType.setUpdateTime(new Date());
            int i = dictTypeMapper.insertSelective(dictType);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
    * @Author Root
    * @Description 字典模糊查询
    * @Date 2023/6/21 14:16
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictName
    */
    @Override
    public CommonResult selectLike(String dictName) {
        Example example = new Example(DictType.class);
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(dictName)){
            example.createCriteria().andLike("dictName","%"+dictName+"%");
        }
        return CommonResult.ok(dictTypeMapper.selectByExample(example));
    }




}
